package net.ikenway.springboot.demo.Controller;

import net.ikenway.springboot.demo.Model.Dto.ActivityDto;
import net.ikenway.springboot.demo.Model.Dto.ResponseBodyDto;
import net.ikenway.springboot.demo.Service.Implement.ActivityServiceImpl;
import net.ikenway.springboot.demo.Utils.ValidateErrorMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/20/2020
 */
@RestController
@RequestMapping("/api/act")
public class ActivityController {
    private final ActivityServiceImpl activityService;

    @Autowired
    ActivityController(ActivityServiceImpl activityService) {
        this.activityService = activityService;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResponseBodyDto findAll(@RequestParam(required = false) Boolean enable) {
        Map<String, Object> data = new HashMap<>();
        List<ActivityDto> activityDtos;
        try {
            if (enable == null) activityDtos = activityService.findAll();
            else activityDtos = activityService.findAllByEnable(enable);
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }

        return new ResponseBodyDto(200, "查询成功", activityDtos);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseBodyDto create(@Valid @RequestBody ActivityDto activityDto, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) throw new Exception(ValidateErrorMsgUtil.getFirstFormatMsg(bindingResult.getAllErrors()));
            activityService.create(activityDto);
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "商品不存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }

        return new ResponseBodyDto(200, "创建成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBodyDto update(@Valid @RequestBody ActivityDto activityDto, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) throw new Exception(ValidateErrorMsgUtil.getFirstFormatMsg(bindingResult.getAllErrors()));

            activityService.update(activityDto);
        } catch (EntityNotFoundException entityNotFoundException){
            return new ResponseBodyDto(500, "活动不存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }

        return new ResponseBodyDto(200, "更新成功");
    }

    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public ResponseBodyDto disable(@RequestBody Map<String, List<Long>> requestData) {
        try {
            if (requestData.get("actIds") == null) throw new Exception("数组不能为空");
            activityService.disable(requestData.get("actIds"));
        } catch (EntityNotFoundException entityNotFoundException){
            return new ResponseBodyDto(500, "活动不存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "停止活动成功");
    }

    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public ResponseBodyDto enable(@RequestBody Map<String, List<Long>> requestData) {
        try {
            if (requestData.get("actIds") == null) throw new Exception("数组不能为空");
            activityService.enable(requestData.get("actIds"));
        } catch (EntityNotFoundException entityNotFoundException){
            return new ResponseBodyDto(500, "活动不存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "启用活动成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseBodyDto delete(@RequestBody Map<String, List<Long>> requestData) {
        try {
            if (requestData.get("actIds") == null) throw new Exception("数组不能为空");
            activityService.delete(requestData.get("actIds"));
        } catch (EntityNotFoundException entityNotFoundException){
            return new ResponseBodyDto(500, "活动不存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "删除活动成功");
    }

}
